package dto;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.YourFavourite;

public class DtoYourFavourite {
	public static List<YourFavourite> getAll(String sql , DataManager dataManager) {
		YourFavourite yf = null;
		List<YourFavourite> yourfavorites = new ArrayList<YourFavourite>() ;
		Connection conn = dataManager.getConnection();
		Statement statement = null;
		ResultSet rs = null;
		if (conn != null) {
			try {
				statement = conn.createStatement();
				
				rs = statement.executeQuery(sql);
				while (rs.next()) {
					yf = new YourFavourite();
					yf.setYourFavoriteId(rs.getInt("favoriteId"));
					yf.setUserId(rs.getInt("userId"));
					yf.setProductId(rs.getInt("productId"));
					
					yourfavorites.add(yf);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {

				try {
					if (rs != null) 
						rs.close();
					if(statement != null)
						statement.close();
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return yourfavorites;
	}
	
	public static List<YourFavourite> getByUserId(int userId , DataManager dataManager)
	{
		String sql = "select * from yourfavourite where userId = "+ userId;
		return getAll(sql, dataManager);
	}
	
	public static boolean insert(YourFavourite yf, DataManager dataManager)
	{
		Connection conn = dataManager.getConnection();
		Statement st = null;
		boolean check = false;
		String sql = "insert into yourfavourite(userId, productId) " +
				"values ("+yf.getUserId()+", "+yf.getProductId()+")";
		if(conn!= null)
		{
			try {
				st = conn.createStatement();
				
				int rs = st.executeUpdate(sql);
				if(rs > 0) 
					check = true;
			} catch (SQLException e) {
				System.out.println("loi cau sql : " + sql);
			}
			finally
			{
				try {
					
					if(st != null)
						st.close();
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return check;
	}
	
	public static boolean deleteYourFavoriteItem(DataManager dataManager, int yourFavoriteId) {
		Connection conn = null;
		Statement statement = null;
		String sqlQuery = "delete from yourfavourite where favoriteId="+yourFavoriteId;
		System.out.print(sqlQuery);
		boolean isOk = true;
		try{
			conn = dataManager.getConnection();
			statement = conn.createStatement();
			statement.executeUpdate(sqlQuery);
		}
		catch (Exception e) {
			System.out.println("loi cau lenh : " + sqlQuery);
			isOk = false;
		}
		finally {
			try {
				dataManager.closeConnection(conn);
				if(statement != null)
					statement.close();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
		}
		return isOk;
	}
	/*
	 * kiem tra xem san pham da ton tai trong favourite hay chua
	 * da co tra ve false 
	 * chua co tra ve true
	 * */
	public static boolean checkProduct(int productId , int userId, DataManager dataManager)
	{
		String sql = "select * FROM yourfavourite where productId = "+productId+" and userId =" + userId;
		return getAll(sql, dataManager).isEmpty()? true:false;
		
	}
	
	



}
